#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

ll gcd(ll a, ll b, ll& x, ll& y) {
  if (!b) {
    x = 1, y = 0;
    return a;
  }
  ll d = gcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ll a, b;
  cin >> a >> b;
  if (a > b) swap(a, b);  // 1 < a < b
  ll x, y;
  gcd(a, b, x, y);
  ll x1 = x % b;
  if (x1 < 0) x1 += b;
  ll y1 = (a * x1 - 1) / b;
  ll y2 = y % b;
  if (y2 < 0) y2 += a;
  ll x2 = (b * y2 - 1) / a;
  ll l = (y1 - 1) * b + (x2 - 1) * a + 1;
  /*
    l is the answer
    1. if a == 2, obvious
    2. if a > 2
      a * x1 - b * y1 = 1
      b * y2 - a * x2 = 1
      a * (x1 - x2) + b * (y2 - y1) = 2 --- I
      easy to see that x1 - x2 and y1 - y1 could not be zero,
      and with opposite sign. ( a + b > 2, a > 2, b > 2)
      provided:
      b * (y1 - 1) + a * (x2 - 1) = l - 1 --- II
      I + II, and we get:
      l + 1 = b * (y2 - 1) + a * (x1 - 1)
      x1 >= 1, y2 >= 1
      l + 1 could be expressed by a and b
  */
  ll h = (y1 - 1) * b + x2 * a - 1;
  cout << l;
  return 0;
  // what if this is not be realized,
  // we could find it by searching from the middle
  while (l <= h) {
    ll mid = (l + h) >> 1;
    __int128_t x = (__int128_t)x1 * mid;
    __int128_t y = (__int128_t)y1 * mid;
    if (x / b * a >= y)
      h = mid - 1;
    else
      l = mid + 1;
  }
  cout << h;
  return 0;
}